package com.kongming.kmdm.ws.rest.msg;

import net.sf.json.JSONObject;

public class SubRequestMsg {

    public static enum CMDTYPE {
        Ping, Discard, Pong
        //QUIT, REG, RECIVEMSG
    };//ping request,discard the request,response for ping

    private CMDTYPE             cmd;

    private String              msg;

    private static final String CMD_KEY = "type";

    private static final String MSG_KEY = "msg";

    public SubRequestMsg (JSONObject json) {

        this.cmd = CMDTYPE.valueOf (json.getString (CMD_KEY));
        this.msg = String.valueOf (json.get (MSG_KEY));
    }

    public static boolean isValidCmd (JSONObject json) {

        if (json.containsKey (CMD_KEY)) {
            String tmpCmd = json.getString (CMD_KEY);
            for (CMDTYPE index : CMDTYPE.values ()) {
                if (index.name ().equals (tmpCmd)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isValidMsg (JSONObject json) {

        return json.containsKey (MSG_KEY);
    }

    public CMDTYPE getCmd () {

        return cmd;
    }

    public void setCmd (CMDTYPE cmd) {

        this.cmd = cmd;
    }

    public String getMsg () {

        return msg;
    }

    public void setMsg (String msg) {

        this.msg = msg;
    }
}
